package jdk.jdk8.lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: zk
 * @since: 2022-09-16 19:59
 */
public class Java8 {
    public static void main(String[] args) {
        // stream 3个操作，1起始操作：stream ，2中间操作，filter,map,distinct(),sorted 3.结束操作 collect,forEach,max,min
        List<Student> students = initData();
        // 1.foreach == for

        // 2.filter true保留，false过滤 + collect
        filter(students);

        // 3 toMap
        //toMap(students);
        // Iterable 集合顶级接口

        //students.forEach();
        //Stream<Student> stream = students.stream().forEach();

        // 4.map
        //List<String> mapList = students.stream().map(Student::getName).collect(Collectors.toList());

        // 5.map + filter
        // 5.1
        //List<Integer> collect =
        LongSummaryStatistics statistics = students.stream()
                //.map(Student::getAge)
                .mapToLong(Student::getAge)// int
                .distinct()
                .filter(Objects::nonNull)
                .filter(age -> age > 19)
                .sorted()
                .summaryStatistics();

        //statistics

        //.collect(Collectors.toList());

        // 等价于下面写法
        //List<Integer> ageList = new ArrayList<>();
        //for (Student student : students) {
        //    ageList.add(student.getAge());
        //}
        //List<Integer> ageResult= new ArrayList<>();
        //for (Integer age : ageList) {
        //    if (Objects.nonNull(age) && age > 19) {
        //        ageResult.add(age);
        //    }
        //}


        List<String> orders = new ArrayList<>();
        for (Student student : students) {
            orders.add(student.getName());
        }

        //mapList.stream().forEach(System.out::println);

        // 场景通过orderNos批量查订单
        //mapper.queryByOrderNos(mapList);


        //foreach1(studentList);

    }

    private static void filter(List<Student> students) {
        List<Student> studentList = students.stream().filter(student -> {
            if (student.getAge() == null) {
                return false;
            }
            return student.getAge() > 19;
        }).collect(Collectors.toList());

        Set<Student> studentSet = students.stream().filter(student -> {
            if (student.getAge() == null) {
                return false;
            }
            return student.getAge() > 19;
        }).collect(Collectors.toSet());
    }

    private static void toMap(List<Student> students) {
        // key:name,value:Student
        //Map<String, Student> studentMap = students.stream().collect(Collectors.toMap(student -> student.getName(), student -> student));
        Map<String, Student> studentMap = students.stream().collect(Collectors.toMap(e -> e.getName(), e -> e, (o1, o2) -> o2));

        // == 3
        studentMap.forEach((k, v) -> {
            System.out.println("key=" + k + " value=" + v);
        });

        // map的遍历
        //mapForeach(studentMap);
    }

    private static void mapForeach(Map<String, Student> studentMap) {
        // 1.keySet
        Set<String> keySet = studentMap.keySet();
        keySet.stream().forEach(s -> {
            System.out.println(studentMap.get(s));
        });

        // 2. values
        studentMap.values().stream().forEach(System.out::println);

        // 3.
        for (Map.Entry<String, Student> entry : studentMap.entrySet()) {
            System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
        }
        // == 3
        studentMap.forEach((k, v) -> {
            System.out.println("key=" + k + " value=" + v);
        });
    }

    private static void foreach1(List<Student> students) {
        students.stream().forEach(student -> {
            System.out.println(student);
            // do something
        });

        // 等价
        for (Student student : students) {
            System.out.println(student);
        }
    }


    public static List<Student> initData() {
        Student s1 = new Student("张三", 19, 80);
        Student s2 = new Student("李四", 19, 50);
        Student s3 = new Student("王五", 23, null);
        Student s4 = new Student("赵六", 16, 90);
        Student s5 = new Student("钱七", null, 99);
        Student s6 = new Student("吴九", 20, 40);//孙八
        Student s7 = new Student("吴九", 21, 88);

        return Arrays.asList(s1, s2, s3, s4, s5, s6, s7);
    }


}
